package com.vgooo.client.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.webkit.WebView;

public class VGWebView extends WebView {
	private boolean canSroll ;
	private GestureDetector detector ;
	public VGWebView(Context context) {
		super(context);
		init();
		// TODO Auto-generated constructor stub
	}
	public VGWebView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
		// TODO Auto-generated constructor stub
	}
	public VGWebView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
		// TODO Auto-generated constructor stub
	}
	
	private void init(){
		detector = new GestureDetector(new VGWebDetector() );
		canSroll = true ;
	}
	
	
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		if(ev.getAction() == MotionEvent.ACTION_UP){
			canSroll = true ;
		}
		return super.onInterceptTouchEvent(ev)&&detector.onTouchEvent(ev);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		if(ev.getAction() == MotionEvent.ACTION_DOWN){
			int temp_Srolly = getScrollY() ;
			scrollTo(getScrollX(), getScrollY() +1) ;
			scrollTo(getScrollX(), temp_Srolly);
		}
		return super.onTouchEvent(ev);
	}
	
	
	
	private class VGWebDetector extends SimpleOnGestureListener{
		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			// TODO Auto-generated method stub
			if (canSroll) {
				if(Math.abs(distanceY)> Math.abs(distanceX)){
					canSroll = true ;
				}else{
					canSroll = false ;
				}
			}
			return canSroll;
		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			// TODO Auto-generated method stub
			return super.onFling(e1, e2, velocityX, velocityY);
		}
		
		
		
	}
	
	private static void log(String info){
		Log.i("VGWebView", info) ;
	}

}
